﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LogLib;
using System.IO;

namespace PhotoNamingService
{
    public class PhotoNamingDefault : PhotoNamingBase
    {
        public PhotoNamingDefault() : base()
        {
           
        }
     
        public override bool GetPhotoName(string inParams, out string PhotoName)
        {
            PhotoName = "";
            try
            {
                PhotoName = GetSerialNumber();
                return true;
            }
            catch (Exception ex)
            {
                _error = ex.ToString();
                Log.WriteException(ex.ToString());
                return false;
            }
        }

        protected string HandleIndex(int index)
        {       
            if (index >= 0 && index <= 999)
            {
                return index.ToString().PadLeft(3, '0');
            }
            else
            {
                throw new Exception("当日序列号已超出指定范围(0~999)");
            }
        }

        protected string GetSerialNumber()
        {
            // KP + 4位机器编号 + yyMMdd + 一位多功能编号+ 3位流水号      
            string serialNumber = "";
            int index = IndexConfig.GetPhotoNameIndex();
            Log.WriteAsync("当前序列号:" + index.ToString());           
            string date = DateTime.Now.ToString("yyyyMMdd");
            date = date.Substring(2, 6);
            string strIndex = HandleIndex(index);
            serialNumber = string.Format("KP{0}{1}{2}{3}", 
                ProductConfig.Instance.DeviceID, 
                date, 
                ProductConfig.Instance.LocalId, 
                strIndex);
            Log.WriteAsync("产生序列号:" + serialNumber);   
            return serialNumber;
        }     
    }
}
